﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using IPMessenger.Common.Model;
using IPMessenger.Common.Uitls;

namespace IPMessenger.BroadCast.Receive.Test
{
    /// <summary>
    /// 接收socket广播
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
            sock.Bind(iep);
            EndPoint ep = (EndPoint)iep; //获取发送广播主机的确切通信ip

            Console.WriteLine("Ready to receive.....");

            byte[] data = new byte[1024];
            int recv = sock.ReceiveFrom(data, ref ep);
            //string stringData = Encoding.Default.GetString(data, 0, recv);
            Messenger msg = CommonTools.Deserialize<Messenger>(new Messenger(), data);
            msg.IP = ep.ToString();

            //Console.WriteLine("received: {0} from: {1}", stringData, ep.ToString());
            Console.WriteLine("received: {0} from: {1}", msg.HostName, msg.IP);

            sock.Close();

            Console.Read();
        }
    }
}
